package com.cl.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cl.db.AppLockOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 程序锁的dao
 * @author Administrator
 *
 */
public class AppLockDao {
	private AppLockOpenHelper helper;
	private Context context;
	public AppLockDao(Context context) {
		helper = new AppLockOpenHelper(context);
		this.context = context;
	}
	public void add(String packName) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("packName", packName);
		db.insert("applock", null, values);
		db.close();
		Intent intent = new Intent();
		intent.setAction("com.itheima.safe.datachanged");
		context.sendBroadcast(intent);
	}
	public void delete(String packName) {
		SQLiteDatabase db =  helper.getReadableDatabase();
		db.delete("applock", "packName=?", new String[]{packName});
		db.close();
		Intent intent = new Intent();
		intent.setAction("com.itheima.safe.datachanged");
		context.sendBroadcast(intent);
	}
	public boolean find(String packName) {
		boolean result = false;
		SQLiteDatabase db =  helper.getReadableDatabase();
		Cursor cursor = db.query("applock", null, "packName=?", new String[]{packName}, null, null, null);
		while(cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		db.close();
		return result;
	}
	public List<String> findAll() {
		List<String> packNames = new ArrayList<String>();
		SQLiteDatabase db =  helper.getReadableDatabase();
		Cursor cursor = db.query("applock", new String[]{"packName"}, null, null, null, null, null,null);
		while(cursor.moveToNext()) {
			String packName = cursor.getString(0);
			packNames.add(packName);
		}
		cursor.close();
		db.close();
		return packNames;
	}
}
